// miniprogram/pages/my/register/index.js
var Mcaptcha = require('../../../utils/mcaptcha')
var Test = require('../../../utils/test')
import {UserModel} from '../../../model/user'
import Toast from '@vant/weapp/toast/toast';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 是否微信授权登录
    isAuth:false,
    radio: '1',
    userInfo:'',
    isRegisted: 0,
    options: [{
      city_id: '001',
      city_name: '预备党员'
    }, {
      city_id: '002',
      city_name: '入党积极分子'
    }, {
      city_id: '003',
      city_name: '共青团员'
    }, {
      city_id: '004',
      city_name: '群众'
    }, {
      city_id: '005',
      city_name: '其它'
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.mcaptcha = new Mcaptcha({
      el: 'canvas',
      width: 80,
      height: 35,
      createCodeImg: ""
    });
    this.test = new Test()
  },
  // 下拉框
  change(e){
    console.log(e)
    console.log(e.detail.id, e.detail.name)
  },
  // 单选框
  onChange(event) {
    this.setData({
      radio: event.detail,
    });
  },
  formSubmit(e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    // return
    this.login(e.detail.value)
  },
  // 手机号登录
  login(data){
    UserModel.phoneLogin(data.phone, data.pwd).then((res) => {
      if(res && res.code == 200) {
        // todo
        console.log('手机号登录成功', res)
        wx.setStorageSync("isRegistered", res.data.isRegistered)
        wx.setStorageSync("user", res.data.user)
        wx.setStorageSync('session_key', res.data.session_key)
        wx.switchTab({
          url: '../index',
        })
      }else {
        console.log('登录失败',res)
        Toast(res.msg)
      }
    }).catch(e=> {
      console.log(e)
    })
  },
  reg() {
    wx.navigateTo({
      url: '../register/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  //刷新验证码
  onTap() {
    this.mcaptcha.refresh();
  },
  validateCode() {
    var res = this.mcaptcha.validate(this.data.imgCode);
    if (this.data.imgCode == '' || this.data.imgCode == null) {
      toast.showToast({
        title: '请输入图形验证码'
      })
      return;
    }
    if (!res) {
      toast.showToast({
        title: '图形验证码错误'
      })
      return;
    }
  },
  // 微信授权登录后跳转 手机注册页面
  bindgetuserinfo(e) {
    let that = this;
    let openid = wx.getStorageSync('openid')
    wx.getUserInfo({
      success: (res) => {
        // 用户授权 同时保存用户信息
        wx.setStorageSync('isAuth', true)
        app.globalData.userInfo = res.userInfo;
        
        UserModel.appletLogin(openid).then((res) => {
          if(res && res.code == 200) {
            // todo
            console.log('微信登录成功', res)
            wx.setStorageSync("isRegistered", res.data.isRegistered)
            wx.setStorageSync("user", res.data.user)
            wx.setStorageSync('session_key', res.data.session_key)
            wx.switchTab({
              url: '../index',
            })
          }else {
            console.log('该微信用户尚未注册',res)
            wx.navigateTo({
              url: '../register/index',
            })
          }
        }).catch(e=> {
          console.log(e)
        })
        
      }
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})